package com.homegoods.login.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.homegoods.login.KGetVerHelper;
import com.homegoods.login.R;
import com.homegoods.login.databinding.LoginActivityReSetPwdBindingImpl;
import com.homegoods.login.presenter.ResetPwdPresenter;
import com.homegoods.login.view.RestPWDView;
import com.homegoods.aroutercomment.ARouterPath;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.kedll.library.base.MyBaseFragmentActivity;
import com.kedll.library.util.KeyBoardUtils;
import com.kedll.library.util.MyLog;
import com.kedll.library.util.StringUtil;

import java.util.Objects;

/**
 * 找回密码
 *
 * @author QiQi
 */
@Route(path = ARouterPath.PATH_REST_PWD)
public class ReSetPwdActivity extends MyBaseFragmentActivity<LoginActivityReSetPwdBindingImpl, ResetPwdPresenter> implements RestPWDView {


    KGetVerHelper kGetVerHelper;

    @Override
    protected void initToolBar(Bundle savedInstanceState) {
        super.initToolBar(savedInstanceState);
        setTitle("找回密码");
    }

    @Override
    protected ResetPwdPresenter getPresenter() {
        kGetVerHelper = new KGetVerHelper(this, mBinding.getVer);
        return new ResetPwdPresenter(this, kGetVerHelper);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (kGetVerHelper != null) {
            kGetVerHelper.restart();
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (kGetVerHelper != null) {
            kGetVerHelper.cancel();
            kGetVerHelper = null;
        }
    }

    @Override
    protected int getLayout() {
        return R.layout.login_activity_re_set_pwd;
    }

    /**
     * 初始化控件事件
     */
    @SuppressLint("CheckResult")
    @Override
    protected void initEvent() {
        RxTextView.afterTextChangeEvents(mBinding.pwd)
                .compose(bindToLifecycle())
                .subscribe(afterTextChangeEvent -> mBinding.resetButton.setEnabled(
                        Objects.requireNonNull(afterTextChangeEvent.editable()).length() > 0
                                && StringUtil.isNotEmpty(mBinding.phoneNum.getText())
                                && StringUtil.isNotEmpty(mBinding.inputVer.getText())
                ));

        RxTextView.afterTextChangeEvents(mBinding.phoneNum)
                .compose(bindToLifecycle())
                .subscribe(afterTextChangeEvent -> mBinding.resetButton.setEnabled(
                        Objects.requireNonNull(afterTextChangeEvent.editable()).length() > 0
                                && StringUtil.isNotEmpty(mBinding.pwd.getText())
                                && StringUtil.isNotEmpty(mBinding.inputVer.getText())
                ));

        RxTextView.afterTextChangeEvents(mBinding.inputVer)
                .compose(bindToLifecycle())
                .subscribe(afterTextChangeEvent -> mBinding.resetButton.setEnabled(
                        Objects.requireNonNull(afterTextChangeEvent.editable()).length() > 0
                                && StringUtil.isNotEmpty(mBinding.pwd.getText())
                                && StringUtil.isNotEmpty(mBinding.phoneNum.getText())
                ));
        mBinding.setResetPwd(mPresenter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (kGetVerHelper != null) {
            kGetVerHelper.stop();
        }
    }

    @Override
    public void showLoad(String msg) {
        MyLog.getInstance().showProgressDialog(this, msg);
    }

    @Override
    public void hideLoad() {
        MyLog.getInstance().dismissDialog();
    }

    @Override
    public Context getContext() {
        return null;
    }

    @Override
    public void resetSuc() {
        finish();
    }

    @Override
    public void hideKeyBoard() {
        KeyBoardUtils.hideKeyBoard(this);
    }
}
